package org.gesplan.controller;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.gesplan.entity.WorkLocation;
import org.gesplan.exceptions.NonexistentEntityException;
import org.gesplan.util.Utils;


/**
 *
 * @author David
 */
public class WorkLocationJpaController {

    private EntityManagerFactory emf = null;    

	
    public WorkLocationJpaController() {
        this.emf = Utils.getEntityManagerFactory();
    }


    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }


    public void create(WorkLocation workLocation) {
        
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(workLocation);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        
    }


    public void edit(WorkLocation workLocation) throws NonexistentEntityException, Exception {
        
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            workLocation = em.merge(workLocation);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = workLocation.getId();
                if (findWorkLocation(id) == null) {
                    throw new NonexistentEntityException("The workLocation with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
        
    }


    public void destroy(Integer id) throws NonexistentEntityException {
        
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            WorkLocation workLocation;
            try {
                workLocation = em.getReference(WorkLocation.class, id);
                workLocation.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The workLocation with id " + id + " no longer exists.", enfe);
            }
            em.remove(workLocation);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        
    }


    public List<WorkLocation> findWorkLocationEntities() {
        return findWorkLocationEntities(true, -1, -1);
    }


    public List<WorkLocation> findWorkLocationEntities(int maxResults, int firstResult) {
        return findWorkLocationEntities(false, maxResults, firstResult);
    }


    @SuppressWarnings({ "rawtypes", "unchecked" })
	private List<WorkLocation> findWorkLocationEntities(boolean all, int maxResults, int firstResult) {
            
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(WorkLocation.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
        
    }


    public WorkLocation findWorkLocation(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(WorkLocation.class, id);
        } finally {
            em.close();
        }
    }


    @SuppressWarnings({ "rawtypes", "unchecked" })    
    public int getWorkLocationCount() {
        
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<WorkLocation> rt = cq.from(WorkLocation.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
        
    }
    
    
}